package com.lw.yygh.msm.mq;

import com.alibaba.fastjson.JSONObject;
import com.lw.yygh.model.order.OrderMqVo;
import com.lw.yygh.msm.service.MsmService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class Client {
    @Autowired
    private MsmService msmService;
    @Autowired
    private RedisTemplate redisTemplate;
    @JmsListener( destination = "${mytopic}")
    public void receive(TextMessage textMessage) throws JMSException {
        //判断消息是否重复消费
        Object obj = redisTemplate.opsForValue().get( textMessage.getJMSMessageID()+1 );
        if (obj!=null){
            return;
        }
        OrderMqVo orderMqVo = JSONObject.parseObject( textMessage.getText(), OrderMqVo.class );
        //发送短信
        msmService.send(orderMqVo);
        //存入redis
        redisTemplate.opsForValue().set( textMessage.getJMSMessageID()+1,textMessage.getText(),5, TimeUnit.MINUTES );
    }
}
